﻿using System;
using SD.Common.PoweredByLee;
using SlamDunk.CMS.Model.Base;
using SlamDunk.CMS.Model.ViewModel;

namespace SlamDunk.CMS.Model.DataModel
{
    /// <summary>
    /// 文章
    /// </summary>
    public class Article : BaseEntity
    {
        #region # 属性

        #region 栏目Id —— int ChannelId
        /// <summary>
        /// 栏目Id
        /// </summary>
        public int ChannelId { get; set; }
        #endregion

        #region 用户Id —— int UserId
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId { get; set; }
        #endregion

        #region 标题 —— string Title
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        #endregion

        #region 内部标题 —— string Intitle
        /// <summary>
        /// 内部标题
        /// </summary>
        public string Intitle { get; set; }
        #endregion

        #region 子标题 —— string SubTitle
        /// <summary>
        /// 子标题
        /// </summary>
        public string SubTitle { get; set; }
        #endregion

        #region 作者 —— string Author
        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; set; }
        #endregion

        #region 来源 —— string Source
        /// <summary>
        /// 来源
        /// </summary>
        public string Source { get; set; }
        #endregion

        #region 内容 —— string Content
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }
        #endregion

        #region 图片路径 —— string ImagePath
        /// <summary>
        /// 图片路径
        /// </summary>
        public string ImagePath { get; set; }
        #endregion

        #region 缩略图路径 —— string ThumbPath
        /// <summary>
        /// 缩略图路径
        /// </summary>
        public string ThumbPath { get; set; }
        #endregion

        #region 附件 —— string Attachment
        /// <summary>
        /// 附件
        /// </summary>
        public string Attachment { get; set; }
        #endregion

        #region 关键字 —— string Keywords
        /// <summary>
        /// 关键字
        /// </summary>
        public string Keywords { get; set; }
        #endregion

        #region 上一篇 —— string PrevPage
        /// <summary>
        /// 上一篇
        /// </summary>
        public string PrevPage { get; set; }
        #endregion

        #region 下一篇 —— string NextPage
        /// <summary>
        /// 下一篇
        /// </summary>
        public string NextPage { get; set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
        #endregion

        #region 保存路径 —— string SaveDirectory
        /// <summary>
        /// 保存路径
        /// </summary>
        public string SaveDirectory { get; set; }
        #endregion

        #region 文件名 —— string FileName
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }
        #endregion

        #region 文件扩展名 —— string FileExtName
        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string FileExtName { get; set; }
        #endregion

        #region 排序 —— int? Sort
        /// <summary>
        /// 排序
        /// </summary>
        public int? Sort { get; set; }
        #endregion

        #region 是否已发布 —— bool? Publish
        /// <summary>
        /// 是否已发布
        /// </summary>
        public bool? Publish { get; set; }
        #endregion

        #region 是否是链接 —— bool? IsLink
        /// <summary>
        /// 是否是链接
        /// </summary>
        public bool? IsLink { get; set; }
        #endregion

        #region 是否置顶 —— bool? IsTop
        /// <summary>
        /// 是否置顶
        /// </summary>
        public bool? IsTop { get; set; }
        #endregion

        #region 点击量 —— int? Click
        /// <summary>
        /// 点击量
        /// </summary>
        public int? Click { get; set; }
        #endregion

        #region 导航属性 - 栏目 —— Channel Channel
        /// <summary>
        /// 导航属性 - 栏目
        /// </summary>
        public virtual Channel Channel { get; set; }
        #endregion

        #region 导航属性 - 用户 —— User User
        /// <summary>
        /// 导航属性 - 用户
        /// </summary>
        public virtual User User { get; set; }
        #endregion

        #endregion

        #region # 方法

        #region 转换为视图模型（展示文章） —— ArticleViewForRead ToViewModel()
        /// <summary>
        /// 转换为视图模型（展示文章）
        /// </summary>
        /// <returns>视图模型（展示文章）</returns>
        public ArticleViewForRead ToViewModel()
        {
            return new ArticleViewForRead()
            {
                Id = this.Id,
                Channel = this.Channel,
                Title = this.FormatTitle(),
                Intitle = this.Intitle,
                SubTitle = this.SubTitle,
                Author = this.Author,
                Source = this.Source,
                Content = this.FormatContent(),
                Sort = this.Sort,
                Publish = Convert.ToBoolean(this.Publish) ? "已发" : "未发",
                IsTop = Convert.ToBoolean(this.IsTop) ? "是" : "否",
                Click = this.Click,
                ImagePath = this.ImagePath,
                ThumbPath = this.ThumbPath,
                Attachment = this.Attachment,
                AddTime = this.AddTime.ToString("yyyy-MM-dd"),
                Url = this.GetUrl()
            };
        }
        #endregion

        #region 转换为视图模型（编辑文章） —— ArticleViewForUpdate ToUpdateModel()
        /// <summary>
        /// 转换为视图模型（编辑文章）
        /// </summary>
        /// <returns>视图模型（编辑文章）</returns>
        public ArticleViewForUpdate ToUpdateModel()
        {
            return new ArticleViewForUpdate()
            {
                Id = this.Id,
                UserId = this.UserId,
                Title = this.Title,
                Intitle = this.Intitle,
                SubTitle = this.SubTitle,
                Author = this.Author,
                Source = this.Source,
                Content = this.Content,
                IsTop = this.IsTop,
                ImagePath = this.ImagePath,
                ThumbPath = this.ThumbPath,
                Attachment = this.Attachment
            };
        }
        #endregion

        #region 获取Url地址 —— string GetUrl()
        /// <summary>
        /// 获取Url地址
        /// </summary>
        /// <returns>Url地址</returns>
        public string GetUrl()
        {
            if (this.IsLink == null || this.IsLink == false)
            {
                return string.Format("{0}{1}{2}{3}", this.Channel.SaveDirectory, this.SaveDirectory, this.FileName, this.FileExtName);
            }

            return this.Content;
        }

        #endregion

        #region 获取格式化文章标题 —— string FormatTitle()
        /// <summary>
        /// 获取格式化文章标题
        /// </summary>
        /// <returns>格式化后的文章标题</returns>
        private string FormatTitle()
        {
            string title = this.Title.FilterHtml();     //去除HTML标签
            if (title.Length > 22)
            {
                title = string.Format("{0}...", title.Substring(0, 21));
                return title;
            }

            return title;
        }
        #endregion

        #region 获取格式化文章内容 —— string FormatContent()
        /// <summary>
        /// 获取格式化文章内容
        /// </summary>
        /// <returns>文章内容</returns>
        private string FormatContent()
        {
            string content = this.Content.FilterHtml();     //去除HTML标签
            if (content.Length > 95)
            {
                content = string.Format("{0}...", content.Substring(0, 94));
                return content;
            }

            return content;
        }
        #endregion

        #region 获取文章链接字符串 —— string GetLinkString()
        /// <summary>
        /// 获取文章链接字符串
        /// </summary>
        /// <returns>文章链接字符串</returns>
        public string GetLinkString()
        {
            string link = "<a href='{0}'>{1}</a>";
            return string.Format(link, this.GetUrl(), this.Title.FilterHtml());
        }
        #endregion

        #endregion
    }
}
